/*
 * Copyright (c) 2011, Yawning <yawninglol at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew.protwarrior;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationEventAction;

public class ConcussionBlowAction extends SimulationEventAction {
	ProtWarriorSimulator mState;
	double mCooldownExpireTime = 0.0;

	ConcussionBlowAction(ProtWarriorSimulator state) {
		super(state.mSimEngine);

		mState = state;
		if (mState.mModel.mToon.getTalent(ProtWarriorModel.CONCUSSIONBLOW) == 0) mCooldownExpireTime = Double.MAX_VALUE;
	}

	@Override
	public double perform() {
		int cost = mState.mModel.concussionBlowCost;

		if (!mState.hasRage(cost)) return SimulationAction.ACTION_FAIL;
		if (mCooldownExpireTime > mState.mSimEngine.getSimTime()) return SimulationAction.ACTION_FAIL;

		AttackResult result = mState.mModel.getFrontalYellowResult(mState.mRecklessnessAction.mBonusYellowCrit);

		if (result == AttackResult.MISS) {
			mNrConcussionBlowMiss++;

			if (mState.mIsLogging) mState.mLog.log("Concussion Blow: MISS\n");
		} else if (result == AttackResult.DODGE) {
			mNrConcussionBlowDodge++;

			if (mState.mIsLogging) mState.mLog.log("Concussion Blow: DODGE\n");
		} else if (result == AttackResult.PARRY) {
			mNrConcussionBlowParry++;

			if (mState.mIsLogging) mState.mLog.log("Concussion Blow: PARRY\n");
		} else {
			double damage = mState.mModel.getAttackPower() * mState.mModel.mLevelDep.getConcussionBlowCoeff();

			if (result == AttackResult.CRIT) {
				mNrConcussionBlowCrit++;
				damage *= mState.mModel.multCrit;
			} else {
				mNrConcussionBlowHit++;
			}

			damage *= mState.getDirectDamageMult();
			mConcussionBlowDamage += Math.round(damage);

			if (mState.mIsLogging) mState.mLog.log("Concussion Blow: %s Damage: %d\n", result, Math.round(damage));

			if (result == AttackResult.CRIT) {
				if (mState.mDeepWoundsEv.mIsEnabled) mState.mDeepWoundsEv.onTrigger();
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.YELLOWCRIT);
			} else {
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.YELLOW);
			}

			mState.mActiveTarget.getTargetStunEv().controlledStun(mState.mModel.concussionBlowStunDuration);
		}

		mCooldownExpireTime = mState.mSimEngine.getSimTime() + mState.mModel.concussionBlowCDDuration;

		mState.onRageLoss(cost);

		return 1.5;
	}

	long mConcussionBlowDamage = 0;
	int mNrConcussionBlowMiss = 0;
	int mNrConcussionBlowDodge = 0;
	int mNrConcussionBlowParry = 0;
	int mNrConcussionBlowCrit = 0;
	int mNrConcussionBlowHit = 0;
}
